package com.project.purchase.Controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.purchase.Service.IGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author VX5
 * @Title: QZP
 * @ProjectName purchase
 * @Description: TODO
 * @date ${DAT}20:05
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    IGoods iGoods;

    @GetMapping("/get")
    public String GetAll(HttpServletRequest request,
                         @RequestParam(defaultValue = "1") Integer pageNum,
                         @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        PageHelper.startPage(pageNum,pageSize);
        List list = iGoods.GetAll();
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "goods";
    }

    @PostMapping("/search")
    public String searchByName(HttpServletRequest request,
                               @RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        PageHelper.startPage(pageNum,pageSize);
        String name = request.getParameter("searchname");
        String model = request.getParameter("searchmodel");
        Map<String,Object> map = new HashMap<>();
        map.put("goodsname",name);
        map.put("model",model);
        List list = iGoods.getByNameAndModel(map);
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "goods";
    }

    @RequestMapping(value = "/delet")
    public String deletByID(HttpServletRequest request,
                            @RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "5") Integer pageSize) throws ServletException, IOException {
        PageHelper.startPage(pageNum,pageSize);
        String id = request.getParameter("id");
        iGoods.deletByID(id);
        List list = iGoods.GetAll();
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "goods";
    }
}
